package com.jiaai.web.xiaoke.family;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.exception.ServerException;
import com.jiaai.family.api.TFamilyImgApi;
import com.jiaai.family.model.TFamilyimgVO;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.system.api.ISystemConfigApi;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title FamilyController
 * @since 2017/12/27
 */
@RestController
@RequestMapping("/api/v1/app/familyImg")
public class AppFamilyImgController {
    @Reference
    private TFamilyImgApi tFamilyImgApi;

    @Reference
    private ISystemConfigApi iSystemConfigApi;

    /**
     * 获取所有家族相册列表
     * @param curr
     * @param tFamilyimgVO
     * @return
     * @throws Exception
     */
    @GetMapping("/findByAll")
    @ResponseBody
    public Object findByAll(@RequestParam(defaultValue = "0", required = false) Integer curr, TFamilyimgVO tFamilyimgVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tFamilyImgApi.findByAll(curr, tFamilyimgVO));
    }
    /**
     * 获取单个家族相册
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/findById")
    public Object findById(@RequestParam Integer id) throws ServerException {
        try {
            return RestResultGenerator.genSuccessResult(tFamilyImgApi.findById(id));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }

    /**
     * 删除单个家族
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @TokenIntercept(checkVisitor = true)
    public Object delete(Integer id) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
            if(((TFamilyimgVO)tFamilyImgApi.findById(id)).getmId().equals(accessUser.getUserId())){
                tFamilyImgApi.delete(id);
                return RestResultGenerator.genSuccessResult("删除成功");
            } else {
                throw new ServerException("暂无权限");
            }
        } catch (Exception e) {
            throw new ServerException("删除失败");
        }
    }
    /**
     * 添加家族
     * @param tFamilyimgVO
     * @return
     * @throws Exception
     */
    @PostMapping("/addFamilyImg")
    @TokenIntercept(checkVisitor = true)
    public Object addImg( TFamilyimgVO tFamilyimgVO) throws ServerException {
        try {
            AccessUser user = RequestUtils.getUser();
            tFamilyimgVO.setmId(user.getUserId());
            tFamilyImgApi.addImg(tFamilyimgVO);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }
}
